﻿/*
 *Copyright (c) 2014, Riccardo Cagnasso
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using PasslibNet.Algorithms;

namespace PasslibNet.Utils
{
    /// <summary>
    /// MCF is a "standard that is not" that store an hashed password in a
    /// string altogheter with an identifier of the algorithm used and all
    /// additional parameters necessary for encoding/veryfing the password
    /// 
    /// The format of an MCF is basically ${agorithmCode}$other$stuff$hash
    /// </summary>
    public static class ModularCryptFormat
    {
        public static readonly Dictionary<string, Type> CodeToAlgo = new Dictionary<string, Type>
        {
	        {"5", typeof(Sha256)},
            {"6", typeof(Sha512)},
            {"pbkdf2-sha1", typeof(Rfc2898)}
        };

        public static readonly Dictionary<Type, string> AlgoToCode = new Dictionary<Type, string>();

        static ModularCryptFormat()
        {
            AlgoToCode = CodeToAlgo.ToDictionary(x => x.Value, x => x.Key);
        }

        /// <summary>
        /// Takes a MCF hash and split it in its components
        /// </summary>
        /// <param name="hash">The hash to split</param>
        /// <returns>
        /// An array of strings, result of splitting the array.
        /// The first should be the code of the algorithm, the last should be the hash
        /// of the password, what's in the middle, depends on the algorithm.
        /// </returns>
        public static string[] SplittedHash(string hash)
        {
            return hash.Split("$".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
        }


        /// <summary>
        /// Parses an hash and instantiate the proper algorithm object.
        /// </summary>
        /// <param name="hash">The hash to be parsed</param>
        /// <param name="initialize">Whether to initialize the created object with parameters from the hash</param>
        /// <returns>A IPasslibAlgorithm with the alhorithm specified in the hash</returns>
        public static IPasslibAlgorithm Factory(string hash, bool initialize=true)
        {
            var words = ModularCryptFormat.SplittedHash(hash);
            var algo = (IPasslibAlgorithm)Activator.CreateInstance(ModularCryptFormat.CodeToAlgo[words[0]]);

            algo.ParseHash(hash);

            return algo;
        }
    }
}
